const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })

exports.main = async (event, context) => {
  const db = cloud.database()
  // 1. 解构参数
  const {
    activeId,
    title,
    time,
    location,
    detail,
    talk,
    latitude,
    longitude,
    pic,  // 接收云文件ID
    publisher,
    publisher_id
  } = event

  try {
    // 2. 生成带签名的临时URL
    const { fileList } = await cloud.getTempFileURL({
      fileList: [pic]
    })
    const picUrl = fileList[0].tempFileURL
    // 1. 执行更新
    const result = await db.collection('actives').doc(activeId).update({
      data: {
        title,
        time,
        location,
        detail,
        talk,
        latitude,
        longitude,
        pic: picUrl,  // 存储带签名的URL
        publisher,
        publisher_id,
        editTime: cloud.database().serverDate(),
      }
    })

    // 2. 返回成功响应
    return { code: 200, message: '更新成功', data: result }
  } catch (err) {
    console.error(err)
    return { code: 500, message: '服务器错误' }
  }
}